import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
// import MusicView from '../views/MusicView.vue'
import LoginView from '../views/LoginView.vue'

// 静态引入
Vue.use(VueRouter)

const routes = [
  {
    path: '/', //根路径
    redirect:'/login' //路由重定向
    // 当匹配到了 / 路径 路由帮我们重新导向到 /music
  },
  {
    path:"/login",
    component:LoginView
  },
  {
    path:'/home',
    component:HomeView,
    children:[
      {
        path:'fashion',
        name:'fashion',
        component:() => import("../views/FashionMusic.vue")
      },
      {
        path:'hong',
        name:'hong',
        component:() => import("../views/HongkongMusic.vue"),
        // 路由独享守卫 只对定义它的路由有效
        beforeEnter:(to,from,next) =>{
          console.log("beforeEnter")
          next()
        }
      },
      {
        path:'japan',
        name:'japan',
        component:() => import("../views/JapanMusic.vue"),
        beforeEnter:(to,from,next) =>{
          console.log("beforeEnter")
          next()
        }
      },

    ]
  },
  {
    path: '/about',
    name: 'abc',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }


]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

// 全局路由前置守卫
// to 即将要进入的路由
// from 即将离开的路由
// next() 放行路由  或者强制跳转到某个路由
// 注意一但定义出来 beforeEach 那么next方法必须被执行
router.beforeEach((to,from,next) =>{
  console.log("beforeEach")
  console.log("to",to)
  console.log("form",from)
  next()
  // if(to.path == '/home/japan'){
  //   next('/login');
  // }else{
  //   next();
  // }
})

// 全局路由解析守卫
router.beforeResolve((to,from,next) =>{
  console.log("beforeResolve")
  next();
})

// 全局路由后置钩子
router.afterEach((to,from) =>{
  console.log("aftereach",to,from)
})



// 全局
// beforeEach
// beforeResolve
// afterEach
// 独享
// beforeEnter
// 组件内
// beforeRouteEnter
// beforeRouteUpdate
// beforeRouteLeave

// 页面首次进入时：
// 1.beforeEach => 2.beforeEnter => 3.beforeRouteEnter => 4.beforeResolve => 5.afterEach

// 1.路由配置
// 2.路由跳转
// 3.嵌套路由
// 4.命名视图
// 5.路由守卫


// history 刷新后会报404 因为vue是单页面应用
// 解决 ：在服务器里面nginx 中配置一下 就行
// location / {
//   try_files $uri $uri/ /index.html;
// }

export default router
